package com.live.dengdengtv.fragment;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.support.CurLiveInfo;
import com.live.dengdengtv.activity.live.LiveHelper;
import com.live.dengdengtv.activity.support.GiftShowManager;
import com.live.dengdengtv.activity.support.GiftShowManager.Gift;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.Constants;
import com.live.dengdengtv.net.NetTaskList;
import com.tencent.TIMMessage;
import com.tencent.TIMValueCallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 */
public class GiftFragment extends BaseFragment {

    private ViewPager viewPager;

    private List<Gift> giftList;

    private List<Gift> giftListPage1 = new ArrayList<Gift>();
    private List<Gift> giftListPage2 = new ArrayList<Gift>();
    private List<Gift> giftListPage3 = new ArrayList<Gift>();

    private List<GridView> gridViewList = new ArrayList<GridView>();

    private Gift preSendGift;

    private GiftShowManager giftShowManager;
    private LiveHelper mLiveHelper;
    public static GiftFragment newInstance(GiftShowManager giftShowManager,List<Gift> giftList, LiveHelper liveHelper) {
        GiftFragment fragment = new GiftFragment();
        fragment.giftShowManager = giftShowManager;
        fragment.mLiveHelper = liveHelper;
        fragment.giftList = giftList;
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setGridViewsData();


        View view = inflater.inflate(R.layout.layout_gift, container, false);

        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        viewPager.setAdapter(new GiftPagerAdapter());

        view.findViewById(R.id.send_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendGift();
            }
        });

        return view;
    }

    private void sendGift(){
        int currentPage = viewPager.getCurrentItem();
        int currentGift = (int) gridViewList.get(currentPage).getTag();
        if(currentPage == 0){
            preSendGift = giftListPage1.get(currentGift);
        }else if(currentPage == 1){
            preSendGift = giftListPage2.get(currentGift);
        }else{
            preSendGift = giftListPage3.get(currentGift);
        }

        sendGroupGift();//TODO   调试模式
//        noticeServerGift();
    }
    private void noticeServerGift(){
        HashMap<String,String> params = new HashMap<String, String>();
        params.put("prop_id",preSendGift.id);//道具id
        params.put("sender_id", LoginInfo.getInstance().getUserId());//发送者
        params.put("live_id", CurLiveInfo.getLiveId());//
//        params.putAll(AppInfo.getHeadInfo());
        requestNetData(NetTaskList.send_gift, params,1,false);
        Toast.makeText(getContext(),"正在发送..",Toast.LENGTH_SHORT).show();
   }
    private void sendGroupGift(){
        LoginInfo.getInstance().subtractVirCoin(preSendGift.giftValue);//刷新用户虚拟币
        mLiveHelper.sendGroupMessage(Constants.AVIMCMD_send_gift, preSendGift.id,
                new TIMValueCallBack<TIMMessage>() {
                    @Override
                    public void onError(int i, String s) {
                        Toast.makeText(getContext(),"网络错误..",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(TIMMessage timMessage) {
                        preSendGift.senderId = LoginInfo.getInstance().getUserId();
                        giftShowManager.addGift(preSendGift);
                    }
                });
   }
    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if (id == 1) {
            if(response.result == 0){
                sendGroupGift();
            }else {
                Toast.makeText(getContext(),"网络错误",Toast.LENGTH_SHORT).show();
            }
        }
        return false;
    }
//
//    private void subtractVirCoin(){
//        try {
//            int currCoin = LoginInfo.getInstance().getVirCoin();
//            int value = Integer.parseInt(preSendGift.giftValue);
//            LoginInfo.getInstance().setVirCoin((currCoin-value)+"");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    private void setGridViewsData(){
        for (int i =0;i< 8;i++){
            giftListPage1.add(giftList.get(i));
        }
        for (int i =8;i< 16;i++){
            giftListPage2.add(giftList.get(i));
        }
        for (int i =16;i< 24;i++){
            giftListPage3.add(giftList.get(i));
        }

        final GridView gridView1 = new GridView(getActivity());
        final GridView gridView2 = new GridView(getActivity());
        final GridView gridView3 = new GridView(getActivity());
        final GridViewAdapter gridViewAdapter1 = new GridViewAdapter(giftListPage1, gridView1);
        final GridViewAdapter gridViewAdapter2 = new GridViewAdapter(giftListPage2, gridView2);
        final GridViewAdapter gridViewAdapter3 = new GridViewAdapter(giftListPage3, gridView3);

        gridView1.setNumColumns(4);
        gridView1.setTag(0);
        gridView1.setAdapter(gridViewAdapter1);
        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gridView1.setTag(position);
                gridView2.setTag(-1);
                gridView3.setTag(-1);
                gridViewAdapter1.notifyDataSetChanged();
                gridViewAdapter2.notifyDataSetChanged();
                gridViewAdapter3.notifyDataSetChanged();
            }
        });
        gridViewList.add(gridView1);


        gridView2.setNumColumns(4);
        gridView2.setAdapter(gridViewAdapter2);
        gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gridView1.setTag(-1);
                gridView2.setTag(position);
                gridView3.setTag(-1);
                gridViewAdapter1.notifyDataSetChanged();
                gridViewAdapter2.notifyDataSetChanged();
                gridViewAdapter3.notifyDataSetChanged();
            }
        });
        gridViewList.add(gridView2);


        gridView3.setNumColumns(4);
        gridView3.setAdapter(gridViewAdapter3);
        gridView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gridView1.setTag(-1);
                gridView2.setTag(-1);
                gridView3.setTag(position);
                gridViewAdapter1.notifyDataSetChanged();
                gridViewAdapter2.notifyDataSetChanged();
                gridViewAdapter3.notifyDataSetChanged();
            }
        });
        gridViewList.add(gridView3);

    }


    private FragmentStopLis fragmentStopLis;
    public void setOnStopLis(FragmentStopLis fragmentStopLis){
        this.fragmentStopLis = fragmentStopLis;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(fragmentStopLis!=null){
            fragmentStopLis.onFragmentStop();
            fragmentStopLis = null;

            giftList = null;
            giftListPage1.clear();
            giftListPage1 = null;
            giftListPage2.clear();
            giftListPage2 = null;
            giftListPage3.clear();
            giftListPage3 = null;
        }
    }


    private class GiftPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() { return gridViewList.size();}

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(gridViewList.get(position));
            return gridViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    private class GridViewAdapter extends BaseAdapter{
        private List<GiftShowManager.Gift> mGiftList ;
        private GridView mGridView ;
        public GridViewAdapter(List<Gift> mGiftList, GridView gridView){
            this.mGiftList = mGiftList;
            this.mGridView = gridView;
        }
        @Override
        public int getCount() {
            return mGiftList.size();
//            return 8;
        }

        @Override
        public Gift getItem(int position) {
            return mGiftList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getActivity().getLayoutInflater();

            View view = inflater.inflate(R.layout.gift_item, parent,false);
            ImageView gift_imageView = (ImageView) view.findViewById(R.id.gift_imageView);
            TextView continued_tv = (TextView) view.findViewById(R.id.continued_tv);
            TextView gift_name_tv = (TextView) view.findViewById(R.id.gift_name_tv);
            TextView giftValue_tv = (TextView) view.findViewById(R.id.giftValue_tv);

            gift_imageView.setImageResource(mGiftList.get(position).imageResId);
            continued_tv.setVisibility(mGiftList.get(position).continued ? View.VISIBLE:View.INVISIBLE);
            gift_name_tv.setText(mGiftList.get(position).giftName);
            giftValue_tv.setText(mGiftList.get(position).giftValue);

//            int selected = mGridView.getSelectedItemPosition();
            int selected = mGridView.getTag() == null? -1:(int)(mGridView.getTag());

//            Log.e("GridViewAdapter", "getSelectedItemPosition=" + selected);
            if(selected == position){
                gift_name_tv.setTextColor(0xFFD2171F);
                giftValue_tv.setTextColor(0xFFD2171F);
            }else {
                gift_name_tv.setTextColor(0xFFFCF8F9);
                giftValue_tv.setTextColor(0xFF999999);
            }

            return view;
        }

    }



}
